package com.daoyi.controller;

import com.daoyi.pojo.*;
import com.daoyi.pojo.vo.*;
import com.daoyi.service.ItemService;
import com.daoyi.utils.ApiResponse;
import com.daoyi.utils.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {

    @Autowired
    private ItemService itemService;

    @Operation(summary = "查询商品详情", description = "查询商品详情", method = "GET")
    @GetMapping("/info/{itemId}")
    public ApiResponse info(
            @Parameter(name = "itemId", description = "商品id", required = true)
            @PathVariable String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return ApiResponse.errorMsg(null);
        }

        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> itemImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);

        return ApiResponse.ok(itemInfoVO);
    }

    @Operation(summary = "查询商品评价等级", description = "查询商品评价等级", method = "GET")
    @GetMapping("/commentLevel")
    public ApiResponse commentLevel(
            @Parameter(name = "itemId", description = "商品id", required = true)
            @RequestParam String itemId) {

        if (StringUtils.isBlank(itemId)) {
            return ApiResponse.errorMsg(null);
        }

        CommentLevelCountsVO countsVO = itemService.queryCommentCounts(itemId);

        return ApiResponse.ok(countsVO);
    }

    @Operation(summary = "查询商品评论", description = "查询商品评论", method = "GET")
    @GetMapping("/comments")
    public ApiResponse comments(
            @Parameter(name = "itemId", description = "商品id", required = true)
            @RequestParam String itemId,
            @Parameter(name = "level", description = "评价等级", required = false)
            @RequestParam Integer level,
            @Parameter(name = "page", description = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(itemId)) {
            return ApiResponse.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult grid = itemService.queryPagedComments(itemId,
                                                                level,
                                                                page,
                                                                pageSize);

        return ApiResponse.ok(grid);
    }

    @Operation(summary = "搜索商品列表", description = "搜索商品列表", method = "GET")
    @GetMapping("/search")
    public ApiResponse search(
            @Parameter(name = "keywords", description = "关键字", required = true)
            @RequestParam String keywords,
            @Parameter(name = "sort", description = "排序", required = false)
            @RequestParam String sort,
            @Parameter(name = "page", description = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return ApiResponse.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }

        PagedGridResult grid = itemService.searhItems(keywords,
                                                        sort,
                                                        page,
                                                        pageSize);

        return ApiResponse.ok(grid);
    }

    @Operation(summary = "通过分类id搜索商品列表", description = "通过分类id搜索商品列表", method = "GET")
    @GetMapping("/catItems")
    public ApiResponse catItems(
            @Parameter(name = "catId", description = "三级分类id", required = true)
            @RequestParam Integer catId,
            @Parameter(name = "sort", description = "排序", required = false)
            @RequestParam String sort,
            @Parameter(name = "page", description = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (catId == null) {
            return ApiResponse.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }

        PagedGridResult grid = itemService.searhItems(catId,
                sort,
                page,
                pageSize);

        return ApiResponse.ok(grid);
    }

    // 用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格），类似京东淘宝
    @Operation(summary = "根据商品规格ids查找最新的商品数据", description = "根据商品规格ids查找最新的商品数据", method = "GET")
    @GetMapping("/refresh")
    public ApiResponse refresh(
            @Parameter(name = "itemSpecIds", description = "拼接的规格ids", required = true, example = "1001,1003,1005")
            @RequestParam String itemSpecIds) {

        if (StringUtils.isBlank(itemSpecIds)) {
            return ApiResponse.ok();
        }

        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);

        return ApiResponse.ok(list);
    }
}
